package com.sirdc.modules.sys.web.threeSevenAndInterView;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.threeSeven.filter.IndexDataFilter;
import net.huashitong.threeSeven.filter.RequestModelFilter;
import net.huashitong.threeSeven.service.IndexDataService;
import net.huashitong.threeSeven.service.IndexWithAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web/thirdIndustry")
public class ThreeSevenController extends JsonBaseController {

    @Autowired
    private IndexDataService indexDataService;
    @Autowired
    private IndexWithAreaService indexWithAreaService;

    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 三产七大指标       首页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getindex", method = RequestMethod.POST)
    public Message getIndex(@RequestBody IndexDataFilter filter) {
        Map<String, Object> result = indexDataService.getWebIndex(filter);
        if (ObjectUtils.isNotBlank(result)) {
            return coverMessage("200", "获取成功", result);
        } else {
            return coverMessage("201", "获取失败，无数据");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getindexmore", method = RequestMethod.POST)
    public Message getIndexMore(@RequestBody IndexDataFilter filter) {
        Map<String, Object> result = indexDataService.getWebIndexDetail(filter);
        if (ObjectUtils.isNotBlank(result)) {
            return coverMessage("200", "获取成功", result);
        } else {
            return coverMessage("201", "获取失败，无数据");
        }
    }

    /**
     * 三产七大指标       二级
     * 参数：indexCode（必填）、dateTime（必填）
     * 第一张图，截止时间：deadline(非必填)
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getindexdetail", method = RequestMethod.POST)
    public Message getIndexDetail(@RequestBody RequestModelFilter filter) {
        Map<String, Object> resultMap = new HashMap<>();
        if (ObjectUtils.isBlank(filter.getIndexCode())) {
            filter.setIndexCode("A001");
        }
        if ("A001".equals(filter.getIndexCode())) {
            Map<String, Object> resTwo = indexWithAreaService.getIndexDetailTwoInWeb(filter);
            resultMap.put("resTwo", resTwo);
        }
        //==================================1 图表带地区===========================================
        Map<String, Object> resOne = indexWithAreaService.getIndexDetailOne(filter);
        List<Map<String, String>> listOne = (List<Map<String, String>>) resOne.get("resultList");
        List<String> areaNameList = new ArrayList<>();
        List<String> increaseList = new ArrayList<>();
        List<String> totalList = new ArrayList<>();
        for (Map<String, String> one : listOne) {
            areaNameList.add(StringUtils.isBlank(one.get("areaName")) ? "0" : one.get("areaName"));
            increaseList.add(StringUtils.isBlank(one.get("increase")) ? "0" : one.get("increase"));
            totalList.add(StringUtils.isBlank(one.get("total")) ? "0" : one.get("total"));
        }
        resOne.put("areaNameList", areaNameList);
        resOne.put("increaseList", increaseList);
        resOne.put("totalList", totalList);
        resOne.put("indexName", listOne.get(0).get("indexName"));
        resOne.put("indexCode", listOne.get(0).get("indexCode"));
        resOne.put("totalUnit", listOne.get(0).get("totalUnit"));
        resOne.put("increaseUnit", listOne.get(0).get("increaseUnit"));
        resOne.remove("resultList");
        //=========================================================================================
        //==================================3 图表带时间===========================================
        Map<String, Object> resThree = indexWithAreaService.getIndexDetailThree(filter);
        List<Map<String, String>> listThree = (List<Map<String, String>>) resThree.get("resultList");
        List<String> jdTimeList = new ArrayList<>();
        List<String> increaseList3 = new ArrayList<>();
        List<String> totalList3 = new ArrayList<>();
        for (Map<String, String> three : listThree) {
            jdTimeList.add(dateToJuTime(three.get("jdTime")));
            increaseList3.add(StringUtils.isBlank(three.get("increase")) ? "0" : three.get("increase"));
            totalList3.add(StringUtils.isBlank(three.get("total")) ? "0" : three.get("total"));
        }
        resThree.put("jdTimeList", jdTimeList);
        resThree.put("increaseList3", increaseList3);
        resThree.put("totalList3", totalList3);
        resThree.put("indexName", listThree.get(0).get("indexName"));
        resThree.put("indexCode", listThree.get(0).get("indexCode"));
        resThree.put("totalUnit", listThree.get(0).get("totalUnit"));
        resThree.put("increaseUnit", listOne.get(0).get("increaseUnit"));
        resThree.remove("resultList");
        //========================================================================================

        resultMap.put("resOne", resOne);
        resultMap.put("resThree", resThree);

        return coverMessage("200", "成功获取数据", resultMap);
    }

    /**
     * 第三级页面
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryThirdYeMian", method = RequestMethod.POST)
    public Message queryThirdYeMian(@RequestBody RequestModelFilter filter) {
        Map<String, Object> result = null;

        if ("1".equals(filter.getType())) {
            result = indexWithAreaService.queryThirdGrade(filter);
        } else {
            if (filter.getIndexCode().length() > 4) {
                filter.setIndexCode(filter.getIndexCode().substring(0, 4));
                result = indexWithAreaService.queryThirdGrade(filter);

            } else {
                result = indexWithAreaService.queryThirdGrade(filter);
            }
        }

        return coverMessage("200", "成功获取数据", result);
    }

    //201703转成2017第一季度 工具
    public String dateToJuTime(String date){
        String jd = "";
        if("03".equals(date.substring(4,6))){
            jd = "第一季度";
        }
        if("06".equals(date.substring(4,6))){
            jd = "第二季度";
        }
        if("09".equals(date.substring(4,6))){
            jd = "第三季度";
        }
        if("12".equals(date.substring(4,6))){
            jd = "第四季度";
        }
        return date.substring(0,4)+jd;
    }
}
